﻿using System;
using System.Collections.Generic;
using System.Linq;
using XBMCRemote.DataModel.Entities;

namespace XBMCRemote.UseCase
{
    /// <summary>
    /// The use case for all configuration related items.
    /// </summary>
    public class ConfigurationUseCase : BaseUseCase<Configuration>
    {
        /// <summary>
        /// Constructor of the configuration use case.
        /// Create the Configuration table here.
        /// </summary>
        public ConfigurationUseCase()
        {
            CreateTable();
        }

        /// <summary>
        /// Create the configuration table if it doesn't exist and add a default configuration.
        /// </summary>
        private void CreateTable()
        {
            if (Database != null)
            {
                if (Database.Table<Configuration>() == null)
                {
                    Database.CreateTable<Configuration>();
                    Database.Table<Configuration>().Add(DefaultConfiguration);
                    Database.Save();
                }
            }
        }

        /// <summary>
        /// Add a new configuration to the database.
        /// </summary>
        /// <param name="configuration">The new configuration to add.</param>
        public void AddNewConfiguration(Configuration configuration)
        {
            RemoveCurrentSelectedConfiguration();
            this.Persist(configuration);
        }

        /// <summary>
        /// Update an existing configuration.
        /// </summary>
        /// <param name="configuration">The updated configuration.</param>
        public void UpdateConfiguration(Configuration configuration)
        {
            Configuration currentConfiguration = Database.Table<Configuration>().FirstOrDefault(cfg => cfg.Id == configuration.Id);

            this.Update(currentConfiguration, configuration);
        }

        /// <summary>
        /// Change the default configuration.
        /// </summary>
        public void RemoveCurrentSelectedConfiguration()
        {
            Configuration currentDefaultConfiguration = Database.Table<Configuration>().FirstOrDefault(cfg => cfg.IsSelected);
            if (currentDefaultConfiguration != null)
            {
                currentDefaultConfiguration.IsSelected = false;
                UpdateConfiguration(currentDefaultConfiguration);
            }
        }

        /// <summary>
        /// Get the configuration that is marked as default.
        /// </summary>
        /// <returns></returns>
        public Configuration GetDefaultConfiguration()
        {
            return Database.Table<Configuration>().FirstOrDefault(cfg => cfg.IsSelected);
        }

        /// <summary>
        /// Get the configuration for the specified id.
        /// </summary>
        /// <param name="id">The id of the configuration to find.</param>
        /// <returns></returns>
        public Configuration GetConfigurationById(Guid id)
        {
            return Database.Table<Configuration>().FirstOrDefault(cfg => cfg.Id == id);
        }

        /// <summary>
        /// Get all the available configurations.
        /// </summary>
        /// <returns></returns>
        public IList<Configuration> GetAllConfigurations()
        {
            return Database.Table<Configuration>();
        } 

        private Configuration DefaultConfiguration
        {
            get
            {
                Configuration configuration = new Configuration();
                configuration.Id = Guid.NewGuid();
                configuration.Name = "XBMC Work";
                configuration.Host = "10.57.14.18";
                configuration.Port = "7080";
                configuration.Username = "xbmc";
                configuration.Password = "xbmc";
                configuration.IsSelected = true;
                return configuration;
            }
        }
    }
}
